/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.soa.service.busi.wx;

import com.lianzt.commondata.AbstractCommonData;
import com.lianzt.commondata.DataConvertFactory;
import com.soa.exception.GlobalException;
import com.soa.service.BaseService;
import com.soa.util.SystemUtil;
import com.soa.util.tccgl.WeixinUtil;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.logging.Level;
import javax.annotation.Resource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

/**
 *  提醒归还用车
 *  P90009
 * @author chang
 */
@Service
public class TxhcTask extends BaseService{
    @Resource
    private WeixinUtil wx;
    private final Logger log = LoggerFactory.getLogger(TxhcTask.class);

    @Override
    public String[] keys() {
        return null;
    }

    @Override
    public void execute(AbstractCommonData in, AbstractCommonData inhead, AbstractCommonData out, AbstractCommonData outhead) {
        //检测关注的用户是否存在将要到期的车辆
        List<AbstractCommonData>  list = queryList("query_dqcl", new Object[]{});
        Date date = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH");
        String str_date = sdf.format(date);
        for (int i = 0; i < list.size(); i++) {
            AbstractCommonData abs = list.get(i);
            String jssj = abs.getStringValue("jssj");
            String tx_time = scxx(jssj);
            if(str_date.equals(tx_time)){
                String context="【系统提醒】\n"+
                        "您的用车("+SystemUtil.getColValueDesc("jz_gdyc.cph", abs.getStringValue("cph"))+"),将于一天后到期，请尽快在【工地用车】--【我的用车】中归还或续用该车！";
                try {
                    String code = wx.sendMessage(abs.getStringValue("lrr"), "", "", "text", 13, context);
                    AbstractCommonData ab= DataConvertFactory.getInstanceByJson(code);
                    log.debug("调用企业号发送信息返回:{}"+ab);
                    if("ok".equals(ab.getStringValue("errmsg"))){
                        log.debug("----------------------企业号发送消息成功!");
                        //修改jz_gdyc中消息提醒状态
                        try {
                            update("",new Object[]{"1",abs.getStringValue("id")});
                        } catch (Exception e) {
                            log.error("修改工地用车还车提醒状态失败!", e);
                            throw new GlobalException(e);
                        }
                    }
                } catch (Exception e) {
                    log.error("企业号发送消息失败!", e);
                }
            }
            
        }
    }
    
    /***
     * 计算提醒时间 提前一天提醒 只提醒一次
     * @param time 到期时间
     * @return 
     */
    public String scxx(String time){
        String tx_time="";
        String tx_hh = SystemUtil.getSysConfig("tx_time");
        Date now = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH");
        Calendar now_ca = Calendar.getInstance();
        Calendar date_ca = Calendar.getInstance();
        try {
            Date date = sdf.parse(time);
            now_ca.setTime(now);
            date_ca.setTime(date);
            //获取年份
            int now_year = now_ca.get(Calendar.YEAR);
            int date_year = date_ca.get(Calendar.YEAR);
            //获取月中的天数（几号）
           //int now_day = now_ca.get(Calendar.DAY_OF_YEAR);
            int date_day = date_ca.get(Calendar.DAY_OF_MONTH);
            int date_month = date_ca.get(Calendar.MONTH);
            int date_s_month = date_month-1==0?12:date_month-1;
            //判断是否在同一年
            if(now_year==date_year){
                //获取提醒时间 到期时间的前一天
                if(date_day==1){//1号的前一天是上一个月的30,或者31，或者29
                    if(date_s_month==12||date_s_month==1||date_s_month==3||date_s_month==5||date_s_month==7||date_s_month==8||date_s_month==10){
                        tx_time=now_year+"-"+date_s_month+"-31 "+tx_hh;
                    }else if(date_s_month==4||date_s_month==6||date_s_month==9||date_s_month==11){
                        tx_time=now_year+"-"+date_s_month+"-30 "+tx_hh;
                    }else if(date_s_month==2){
                        //判断当年是平/闰年
                        if((date_year%4==0&&date_year%100!=0)||date_year%400==0){//闰年
                            tx_time=now_year+"-"+date_s_month+"-29 "+tx_hh;
                        }else{
                            tx_time=now_year+"-"+date_s_month+"-28 "+tx_hh;
                        }
                    }
                }    
            }
        } catch (ParseException ex) {
            java.util.logging.Logger.getLogger(TxhcTask.class.getName()).log(Level.SEVERE, null, ex);
        }
        log.debug("-----------------------------------生成提醒时间为："+tx_time);
        return tx_time;
    }

    
}
